#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <signal.h>
#include <sys/socket.h>
#include <netinet/in.h>

int main()
{
	FILE *fp;
	char buf[40];
	struct sockaddr_in servaddr;
	int sockfd, n;
	char str[INET_ADDRSTRLEN];
	socklen_t servaddr_len;
	sockfd = socket(AF_INET, SOCK_DGRAM, 0);
	bzero(&servaddr, sizeof(servaddr));

	servaddr.sin_family = AF_INET;
	inet_pton(AF_INET, "127.0.0.1", &servaddr.sin_addr);
	servaddr.sin_port = htons(6666);
	while(1)
	{
		if ((fp = fopen("/sys/class/net/eth0/carrier", "r")) != NULL)
	   	{  
			while (fgets(buf, sizeof(buf), fp) != NULL) 
			{  
				if (buf[0] == '1')
				{
					sendto(sockfd, "4,net_status", strlen("4,net_status"), 0, (struct sockaddr *)&servaddr, sizeof(servaddr));
				}  
			}  
    	}  
    	else  
    	{  
			perror("Open /sys/class/net/eth0/carrier");
    	}  
    	fclose(fp);
		sleep(120);
	}
	
	return 0;
}
